<?php 
/*
** etc:  控制器
** author: deepe.xiao
** update: 2018-10-10
*/

namespace Core;

class Controller{

	var $get;        //$_GET 
	var $post;       //$_POST
	protected  $mytp;

	function __construct(){
		spl_autoload_register("self::models_autoload");
	}


	/**
	 * 当在控制器中需要用到模型类时,自动调用
	 * 注意自动加载类依靠命名空间规范,请注意与文件夹名称一致
	 * @param $classname
	 */
	private function models_autoload($classname){

		//只加载命名空间内包含models的类
		if(strstr($classname,"models")){
			$classname = str_replace('\\', '/', $classname);

			if( strlen( $classname ) != 0 ) {
				$fdir = FCPATH .$classname. ".php";
				if (!file_exists($fdir)) {
					view_log("FUNC=".__FUNCTION__.',Model Not Exists', 'The model not exists <font color="red">' .$fdir . '</font>');
				}else{
					include_once( $fdir );
				}

			}else{
				view_log('Model null error','Model not allow have null');
			}
		}
	}


	/**
	 * 加载模型层
	 * @param $model 模型名字
	 * @param $flag 是否实例化数据库
	 *
	 */
	function loadModel( $model,  $flag=false , $version_dir = ""  ) {
		$this->Mrecodes = "";
		
		if( $version_dir == "" ){
			$version_dir = config_item( 'sp_version' );
			$version_dir = config_item('pj_name')."_".$version_dir[0];
			
		}

		if( strlen( $model ) != 0 ){
			if ( !file_exists( FCPATH."/app/models/".$version_dir."/".$model.".php" ) ){
				view_log('Model Not Exists','The model not exists <font color="red">'.FCPATH."app/models/".$model.".php".'</font>');
			}
			
			include_once( FCPATH."app/models/".$version_dir."/".$model.".php" );

			$model = "M".$model;
			$this->$model = new $model();
			$this->$model->post = $this->post;
			$this->$model->get = $this->get;
			if( $flag ){
				$this->$model->loadMysql();
			}
		}else{
			view_log('Model null error','Model not allow have null');
		}
		return $this->$model;
	}
	
	//加载管理员模型
	function loadAdminModel( $model,  $flag=false  ){
		$this->Mrecodes = "";
		if( strlen( $model ) != 0 ){
			if ( !file_exists( FCPATH."/app/models/admin/".$model.".php" ) ){
				view_log('Model Not Exists','The model not exists <font color="red">'.FCPATH."app/models/".$model.".php".'</font>');
			}
			include_once( FCPATH."app/models/admin/".$model.".php" );
			$model = "M".$model;
			$this->$model = new $model();
			$this->$model->post = $this->post;
			$this->$model->get = $this->get;
			if( $flag ){
				$this->$model->loadMysql();
			}
		}else{
			view_log('Model null error','Model not allow have null');
		}
		return $this->$model;
	}

	//加载视图层
	function loadView( $view, $param='' ){
		if( strlen( $view ) != 0 ){
			if ( !file_exists( FCPATH."app/views/".$view.".html" ) ){
				view_log('View Not Exists','The model not exists <font color="red">'.FCPATH."app/views/".$view.".html".'</font>');
			}
			$data = $param;             //Ò³ÃæÉÏµÄÊý×é±äÁ¿ÃûÎª data
			include_once( FCPATH."app/views/".$view.".html" );
		}else{
			view_log('View null error','View not allow have null');
		}
	}


	//检测用户是否存在
    function checkUser(){
        $route = new Route();
        $login = $route->autoLoadClass("login");
        $login->checkUser();
    }

    //屏蔽用户
    function only_script(){
        if( PHP_SAPI !== 'cli' ){
            header('http/1.1 403 Forbidden');
            die();
        }
    }



	/**
	 * 显示成功页面的方法
	 * @param $message
	 * @param null $jumpurl
	 * @param int $waitSecond
	 */
    function success($message,$jumpurl=null,$waitSecond = 3){

    	$this->jump($message,$jumpurl,$waitSecond,false);

	}
	function error($message,$jumpurl=null,$waitSecond = 3){

		$this->jump($message,$jumpurl,$waitSecond,true);

	}

	/**
	 * 赋值给页面
	 * @param $name
	 * @param $value
	 */
	function assign($name,$value){
		$this->mytp = loadLibrary("Mytp");
		$this->mytp->assign($name,$value);
	}

	private  function jump($message,$jumpurl,$wait_second = 3,$isError = true){
		$this->mytp = loadLibrary("Mytp");
		if ($isError) {
			$this->mytp->assign("error", $message);
			$this->mytp->assign('jumpUrl',isset($jumpurl)?$jumpurl:"javascript:history.back(-1);");

		}else{
			$this->mytp->assign("message", "$message");
			$this->mytp->assign('jumpUrl',isset($jumpurl)?$jumpurl:config_item("base_url"));
		}
		$this->mytp ->assign("waitSecond","$wait_second");
		$this->mytp -> tp("public/jump");
	}





}
?>
